#######################################################
#A COMMON-SPACE TECHNIQUE FOR VOTERS AND LEGISLATORS
#WITH APPLICATION TO THE 2010/2011 Elections
####################################################### 

##NB - Load the Master file first


##We extract Senators 1&2 and identify unique senators
sen1 <- paste(V513,"-",V206)
sen2 <- paste(V521,"-",V206)

sen1_unique <- unique(sen1)
sen2_unique <- unique(sen2)


##create a new cces variable for senate scaling
cces$sen1_scale <- NA
cces$sen2_scale <- NA


##put all senators in a common variable
sen1tmp <- replace(as.numeric(CC334F), as.numeric(CC334F)>7 | is.na(as.numeric(CC334F)), 999)
sen2tmp <- replace(as.numeric(CC334G), as.numeric(CC334G)>7 | is.na(as.numeric(CC334G)), 999)
senc1 <- replace(as.numeric(CC334H), as.numeric(CC334H)>7 | is.na(as.numeric(CC334H)), 999)
senc2 <- replace(as.numeric(CC334I), as.numeric(CC334I)>7 | is.na(as.numeric(CC334I)), 999)

V513 <- replace(V513, V513=="", "bbbbbbbbbb")
V521 <- replace(V521, V521=="", "cccccccccc")
sen1tmp <- (V513==V548)*senc1 + (V513==V551)*senc2 + (V513!=V548 & V513!=V551)*sen1tmp 
sen2tmp <- (V521==V548)*senc1 + (V521==V551)*senc2 + (V521!=V548 & V521!=V551)*sen2tmp 

cces$senator1 <- sen1tmp
cces$senator2 <- sen2tmp

################################################
#To the matrix of senators, we add individuals,
#parties, and the president
################################################

self <- CC334A
obama <- CC334C
democrats <- CC334D
republicans <- CC334E

placement_matrix <- cbind(self, obama, democrats, republicans)

for (j in 1:ncol(placement_matrix)){
  placement_matrix[,j] <- as.numeric(placement_matrix[,j])
  placement_matrix[,j] <- replace(placement_matrix[,j], placement_matrix[,j]==8 | is.na(placement_matrix[,j]), 999)
}  

###################################################
#Next, we estimate AM step-by-step for each state
###################################################

##We need one overall estimate across all respondents
overall_scale <- aldmck(placement_matrix[,1:4], polarity=2, respondent=1, missing=999, verbose=TRUE)

#save the overall dem and rep pos
dem_overall <- overall_scale$stim[2]
rep_overall <- overall_scale$stim[3]

states_unique <- unique(V206)


##here are the voters' preferences
ideal_points <- overall_scale$resp$ideal

##now senators
placement_holder <- NULL
med_voter <- NULL

for (i in 1:length(states_unique)){
  
  #subset by state
  tmp <- subset(cces, V206==states_unique[i])  
  temp <- cbind(placement_matrix[V206==states_unique[i],], tmp$senator1, tmp$senator2)
  colnames(temp)[5:6] <- c(tmp$V513[1],tmp$V521[1])
  
  #remove stimuli that are not measured
  drop <- NULL
  for (j in 1:ncol(temp)) if(sum(temp[,j]==999, na.rm=TRUE)==nrow(temp)) drop <- c(drop, j)
  if (sum(drop)>0) temp <- temp[,-drop]
  
  scaling <- aldmck(temp, polarity=2, respondent=1, missing=999, verbose=TRUE)
  
  #recale from state space back to overall space
  #using the equ OVERALL = slope*STATE - slope*STATE_DEM + OVERALL_DEM
  
  slope <- (overall_scale$stim[3]-overall_scale$stim[2])/(scaling$stim[3]-scaling$stim[2])
  rescaling <- slope*scaling$stim - slope*scaling$stim[2] + overall_scale$stim[2]
  
  #add results to cces dataset...
  cces$sen1_scale[V206==states_unique[i]] <- rescaling[4]
  cces$sen2_scale[V206==states_unique[i]] <- rescaling[5]
  
  #save results
  placement_holder <- c(placement_holder, rescaling[4:5])
  med_voter <- c(med_voter, rep(mean(ideal_points[V206==states_unique[i]],na.rm=TRUE),2))
}



##Plot the results
plot(1:102, placement_holder, pch="")
text(1:102, placement_holder, names(placement_holder), cex=0.7)


##append the state mean preferences
state_ideal_pt <- tapply(ideal_points, V206, mean, na.rm=TRUE)
text(1:length(state_ideal_pt), state_ideal_pt, names(state_ideal_pt), cex=0.7)


##Group and save the results by state
senator_results <- cbind(placement_holder, med_voter)

